//y引入图片上传工具文件
let { moveFiles, deleteFiles } = require('../utils/handleFiles')
let { getClaims, addClaims, updateClaims, deleteClaimsById, getClaimsById, againReapply, agreeReapply, disagreeReapply} = require('../dao/claimsDao')
//获取数据
module.exports.getClaims = async function (param) {
    let data = await getClaims(param)
    if (data) {
        return {
            code: 1,
            message: '理赔列表获取成功',
            data
        }
    } else {
        return {
            code: 0,
            message: '理赔列表获取失败',
        }
    }
}
//添加
module.exports.addClaims = async function (cla) {
    let data = await addClaims(cla)
    if (data) {
        data.images.forEach(element => {
            moveFiles({
                fromPath: `./public/temp`,
                toPath: `./public/images`,
                filename: element
            })
        });
        deleteFiles(`./public/temp`)
        return {
            message: '新增成功',
            code: 1,
            data
        }
    }
    return {
        message: '新增失败',
        code: 0,
        data
    }
}
//删除
module.exports.deleteClaimsById = async function (_id) {
    let data = await deleteClaimsById(_id)
    if (data) {
        return {
            message: '删除成功',
            code: 1,
            data
        }
    }
    return {
        message: '删除失败',
        code: 0,
        data
    }
}
//获取某个信息
module.exports.getClaimsById = async function (_id) {
    let data = await getClaimsById(_id)
    if (data) {
        return {
            message: '获取成功',
            code: 1,
            data
        }
    }
    return {
        message: '获取失败',
        code: 0,
        data
    }
}
//修改数据
module.exports.updateClaims = async cla => {
    let saveData=cla
    let data = await updateClaims(cla)
    if (data.nModified) {
        saveData.images.forEach(element => {
            moveFiles({
                fromPath: `./public/temp`,
                toPath: `./public/images`,
                filename: element
            })
        });
        deleteFiles(`./public/temp`)
        return {
            message: '新增成功',
            code: 1,
            data
        }
    }
    return {
        message: '新增失败',
        code: 0,
        data
    }
}
//修改状态信息
module.exports.againReapply = async function (_id) {
    let data = await againReapply(_id)
    console.log(data);
    if (data) {
        return {
            message: '修改成功',
            code: 1,
            data
        }
    }
    return {
        message: '修改失败',
        code: 0,
        data
    }
}
//同意
module.exports.agreeReapply = async function (_id) {
    let data = await agreeReapply(_id)
    console.log(data);
    if (data) {
        return {
            message: '同意成功',
            code: 1,
            data
        }
    }
    return {
        message: '同意失败',
        code: 0,
        data
    }
}
//拒绝
module.exports.disagreeReapply = async function (_id) {
    let data = await disagreeReapply(_id)
    console.log(data);
    if (data) {
        return {
            message: '拒绝成功',
            code: 1,
            data
        }
    }
    return {
        message: '拒绝失败',
        code: 0,
        data
    }
}